package com.wits.platform.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import com.wits.platform.common.constants.ViewType;
import com.wits.platform.common.constants.WebConstants;
import com.wits.platform.common.utils.PageUtil;
import com.wits.platform.entity.Menu;
import com.wits.platform.service.MenuService;
import com.wits.platform.service.SysTabService;
import com.wits.platform.web.mvc.PageParam;

@Controller
@RequestMapping(value = "/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService menuService;
	
	@Autowired
	private SysTabService sysTabService;

    @RequestMapping(value = "/toList")
    public String toList(){
    	return "platform/menu/list";
    }
    
	@RequestMapping(value = "/menuTree")
	@ResponseBody
    public String menuTree(){
    	List<Menu> list=menuService.findAll();
    	return list.toString();
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(Model model,@ModelAttribute("page") PageParam page,HttpServletRequest req) {
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(req, "search_");
		Page<Menu> listPage = menuService.findPage(Menu.class,searchParams,page);
		model.addAttribute("listPage", listPage);
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return PageUtil.toAjaxPageResult(listPage,Menu.getJsonConfig());
	}


    @RequestMapping(value = "/view/{viewType}")
    public String toView(Model model,@PathVariable String viewType,@RequestParam String muId){
    	if(ViewType.Add.getView().equals(viewType)){
    		model.addAttribute(WebConstants.COMMAND,new Menu());
    	}else{
    		model.addAttribute(WebConstants.COMMAND,  menuService.getOne(muId));
    	}
		model.addAttribute("langTypes",sysTabService.getEnabledLangTypes());
		model.addAttribute("viewType",viewType);
    	return "platform/menu/form";
    }
    
    
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public Object save(Model model,@ModelAttribute("command") @Valid Menu command,@RequestParam String viewType,BindingResult result, HttpServletRequest req) {
        if(result.hasErrors()) {
            model.addAttribute(WebConstants.COMMAND, command);
            return toView(model,viewType,command.getMuId());
        }
        if(ViewType.Add.getView().equals(viewType)){
	        command.setDateCre(new Date());
	        command.setUserCre(getCurrentUserId());
        }else{
	        command.setDateUpd(new Date());
	        command.setUserUpd(getCurrentUserId());
        }
        try{
	        menuService.save(command);
	        return ajaxDoneSuccess(this.getText("save.success"));
        }catch(Exception e){
        	return ajaxDoneError(this.getText("save.failed"),e);
        }
    }
	
	

	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam String ids) {
		try {
			menuService.deleteInBatch(ids);
			return ajaxDoneSuccess(this.getText("delete.success"));
		} catch (Exception e) {
			return this.ajaxDoneError(this.getText("delete.failed"),e);
		}
	}


	@RequestMapping(value = "/menuJson")
	@ResponseBody
	public String menuJson() {
		List<Menu> menuList = menuService.findAll();
		return menuList.toString();
	}

	
	@RequestMapping(value = "/checkMuId")
	@ResponseBody
	public boolean checkUniqueMuId(@RequestParam("muId") String muId,@RequestParam("oldMuId") String oldMuId) {
		if (muId.equals(oldMuId)) {
			return true;
		}
		Menu menu = menuService.getOne(muId);
		return menu == null;
	}

	@RequestMapping(value = "/checkMuHref")
	@ResponseBody
	public boolean checkUniqueHref(@RequestParam("muHref") String muHref,@RequestParam("oldMuHref") String oldMuHref) {
		if (muHref.equals(oldMuHref)) {
			return true;
		}
		List<Menu> list= menuService.findByMuHref(muHref);
		return list == null || list.size()==0;
	}

	/**
	 * 授权调用
	 * @return
	 */ 
	@RequestMapping(value = "/rightTree")
	@ResponseBody
	public String rightTree(@RequestParam("roleId") String roleId){
		return menuService.getRightTree(roleId);
	}

}
